;***********************************
; Routines to enable A20 (A20.inc)
; Reindeer OS
;***********************************

bits 16

; Enables a20 through output port

EnableA20_Kb_Out:
	cli
	pusha
		call	wait_input
		mov		al, 0xAD
		out		0x64, al				; disable keyboard
		call	wait_input

		mov		al, 0xD0
		out		0x64, al				; we want read output port
		call	wait_output

		in		al, 0x60
		push	eax						; we store data from output port
		call	wait_input

		mov		al, 0xD1
		out		0x64, al				; tell controller to write output port
		call	wait_input

		pop		eax
		or		al, 2					; set bit 1 (enable A20)
		out		0x60, al				; write out data bact to output port

		call	wait_input
		mov		al, 0xAE				; enable keyboard
		out		0x64, al

		call	wait_input
	popa

	sti
	ret

	wait_input:						; wait for input buffer to be clear
		in		al, 0x64
		test	al, 2
		jnz		wait_input
		ret

	wait_output:
		in		al, 0x64			; wait for output buffer to be clear
		test	al, 1
		jz		wait_output
		ret